package com.example.asus_pc.myshop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.example.asus_pc.myshop.application.MyApp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class AbsCommonAdapter<T> extends BaseAdapter {
    protected       LayoutInflater       mInflater;
    protected       Context              mContext;
    protected       List<T>              mDatas;
    protected final int                  mItemLayoutId;
    protected       MyApp                app;


    public AbsCommonAdapter(Context context, int itemLayoutId) {
        this(context, itemLayoutId, null);
    }

    public AbsCommonAdapter(Context context, int itemLayoutId, MyApp app) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mItemLayoutId = itemLayoutId;
        this.app = app;
        mDatas = new ArrayList<T>();
    }

    public void addItemData(T mBean, boolean isRefresh) {

        mDatas.add(mBean);
        if (isRefresh) {
            notifyDataSetChanged();
        }
    }

    public void addItemData(T mBean, int index, boolean isRefresh) {

        mDatas.add(index, mBean);
        if (isRefresh) {
            notifyDataSetChanged();
        }
    }

    public void addItemData(Collection<? extends T> mCommonbeans, int index, boolean isRefresh) {

        mDatas.addAll(index, mCommonbeans);
        if (isRefresh) {
            notifyDataSetChanged();
        }
    }

    public void addItemData(Collection<? extends T> mCommonbeans, boolean isRefresh) {

        mDatas.addAll(mCommonbeans);
        if (isRefresh) {
            notifyDataSetChanged();
        }
    }

    public void addData(List<T> mCommonbeans, boolean isMore) {

        if (isMore) {
            if (mCommonbeans != null) {

                mDatas.addAll(mCommonbeans);
            }
        } else {
            mDatas.clear();
            if (mCommonbeans != null) {
                mDatas.addAll(mCommonbeans);
            }
        }
        notifyDataSetChanged();
    }

    public void addData(List<T> mCommonbeans) {
        addData(mCommonbeans, false);
    }
    public void remove(int pos) {
        if (mDatas != null && mDatas.size() > 0) {
            mDatas.remove(pos);
            notifyDataSetChanged();
        }
    }

    public void clearData(boolean clear) {
        if (clear) {
            if (mDatas != null && mDatas.size() > 0) {
                mDatas.clear();
                notifyDataSetChanged();
            }
        }
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        if (mDatas != null && position >= 0 && position <= (mDatas.size() - 1)) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final AbsViewHolder viewHolder = getViewHolder(
                position, convertView, parent
        );
        convert(viewHolder, getItem(position), position);
        return viewHolder.getConvertView();

    }

    public abstract void convert(AbsViewHolder helper, T item, int pos);

    private AbsViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        return AbsViewHolder.get(
                mContext, convertView, parent, mItemLayoutId, position
        );
    }

    public List<T> getDatas() {
        return mDatas;
    }

    /**
     * 局部更新数据，调用一次getView()方法；Google推荐的做法
     *
     * @param listView 要更新的listview
     * @param position 要更新的位置
     */
    public void notifyDataSetChanged(ListView listView, int position) {
        /**第一个可见的位置**/
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        /**最后一个可见的位置**/
        int lastVisiblePosition = listView.getLastVisiblePosition();

        /**在看见范围内才更新，不可见的滑动后自动会调用getView方法更新**/
        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
            /**获取指定位置view对象**/
            View view = listView.getChildAt(position - firstVisiblePosition);
            getView(position, view, listView);
        }

    }

}
